package com.hdsx.jxcsxm.interceptor;

import com.hdsx.jxcsxm.log.controller.LoggingBranch;
import com.hdsx.jxcsxm.log.server.LoggingBranchServer;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 占贤银
 * time 2023/8/4 9:33
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Scope("prototype")
@Controller("OperationLogInterceptor")
public class OperationLogInterceptor extends MethodFilterInterceptor {

    @Resource(name = "loggingBranchServerImpl")
    private LoggingBranchServer server;

    private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

    /**
     * name = queryXmlist
     * locale = CN
     * parameters = {xmjbxx.jsxz=农村公路建设,
     * xmjbxx.xmbm=, xmjbxx.knw=,
     * xmjbxx.gydwdm=111013604, xmjbxx.gcfl=,
     * page=1, xmjbxx.jhxdwh=, rows=10, xmjbxx.xmnf=2023, xmjbxx.xzqh=3604, xmjbxx.tsdq=, xmjbxx.xmmc=}
     * url = /jxcsxm/jhcx/queryXmlist.do
     * @param invocation
     * @return
     * @throws Exception
     */
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        LoggingBranch loggingBranch = new LoggingBranch(invocation, request, server);
        THREAD_POOL.submit(loggingBranch);
        final String result = invocation.invoke();
        return result;
    }
}
